/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package almacen;

import static almacen.imagenFacturas.con;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import resources.jcProperties;
import resources.local_connection;

/**
 *
 * @author Homeroe
 */
public class cambiarSucursal extends javax.swing.JFrame {

    /**
     * Creates new form cambiarSucursal
     */
    public cambiarSucursal() {
        try {
            int i=0;
            initComponents();
            con = local_connection.getconnect();
            PreparedStatement st = (PreparedStatement)con.prepareStatement("SELECT * FROM sucursales");
            ResultSet rs = st.executeQuery();
            while(rs.next()){
                comboSucu1.addItem(rs.getString("nombre"));
                if(Almacen.suc1 == rs.getInt("id")) comboSucu1.setSelectedIndex(i);
                comboSucu2.addItem(rs.getString("nombre"));
                if(Almacen.suc2 == rs.getInt("id")) comboSucu2.setSelectedIndex(i);
                i++;
            }
            st = (PreparedStatement)con.prepareStatement("SELECT * FROM sucursales WHERE id="+Almacen.suc1+" OR id="+Almacen.suc2);
            rs = st.executeQuery();
            rs.next();
            String second = rs.getString("nombre");
            String text = rs.getString("nombre");
            if(rs.next())
                text += "/"+rs.getString("nombre");
            else
                text += "/"+second;
            textSucursales.setText(text);
        } catch (SQLException ex) {
Almacen.logger.log(Level.SEVERE, null, ex);        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jProgressBar1 = new javax.swing.JProgressBar();
        textCompany = new javax.swing.JLabel();
        textSucursales = new javax.swing.JLabel();
        comboSucu1 = new javax.swing.JComboBox();
        textSucursal1 = new javax.swing.JLabel();
        textSucursal2 = new javax.swing.JLabel();
        comboSucu2 = new javax.swing.JComboBox();
        buttonOk = new javax.swing.JButton();
        buttonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cambiar Sucursales");
        setIconImage(new ImageIcon(getClass().getResource("/resources/logo_rayito.png")).getImage());

        textCompany.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        textCompany.setText("TeslaBEM");

        textSucursales.setText("Sucursal 1 / Sucursal 2");

        textSucursal1.setText("Sucursal 1");

        textSucursal2.setText("Sucursal 2");

        buttonOk.setText("Aceptar");
        buttonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOkActionPerformed(evt);
            }
        });

        buttonCancel.setText("Cancelar");
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textCompany)
                            .addComponent(textSucursales))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(textSucursal1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(textSucursal2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(15, 15, 15)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboSucu1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(comboSucu2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 228, Short.MAX_VALUE)
                        .addComponent(buttonCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonOk)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(textCompany)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textSucursales)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboSucu1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textSucursal1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textSucursal2)
                    .addComponent(comboSucu2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonOk)
                    .addComponent(buttonCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_buttonCancelActionPerformed

    private void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOkActionPerformed
        Properties propiedades;
        jcProperties editarPropiedades;
        try {
            PreparedStatement st = (PreparedStatement)con.prepareStatement("SELECT id FROM sucursales WHERE nombre='"+comboSucu1.getItemAt(comboSucu1.getSelectedIndex()).toString()+"'");
            ResultSet rs = st.executeQuery();
            //System.out.println("SELECT * FROM sucursales WHERE nombre="+comboSucu1.getItemAt(comboSucu1.getSelectedIndex()).toString()+" OR nombre="+comboSucu2.getItemAt(comboSucu2.getSelectedIndex()).toString());
            rs.next();
            int temp = rs.getInt("id");
            Almacen.suc1 = temp;
            st = (PreparedStatement)con.prepareStatement("SELECT id FROM sucursales WHERE nombre='"+comboSucu2.getItemAt(comboSucu2.getSelectedIndex()).toString()+"'");
            rs = st.executeQuery();
            //System.out.println("SELECT * FROM sucursales WHERE nombre="+comboSucu1.getItemAt(comboSucu1.getSelectedIndex()).toString()+" OR nombre="+comboSucu2.getItemAt(comboSucu2.getSelectedIndex()).toString());
            
            //System.out.println(temp);
            if(rs.next())
                Almacen.suc2 = rs.getInt("id");
            else
                Almacen.suc2 = temp;
            editarPropiedades = new jcProperties();
            propiedades = new Properties();
            propiedades = editarPropiedades.getProperties();
            
            propiedades.setProperty("sucursal1", String.valueOf(Almacen.suc1));
            propiedades.setProperty("sucursal2", String.valueOf(Almacen.suc2));
            FileOutputStream os = null;
            try {
                 os=new FileOutputStream("Propiedades.properties");
                 propiedades.store(os, "Base de Datos Local");
            } catch(IOException ex) {Almacen.logger.log(Level.SEVERE, null, ex); }
            Almacen.checkSameSucursal();
            this.dispose();
        } catch (SQLException ex) {
Almacen.logger.log(Level.SEVERE, null, ex);        }
    }//GEN-LAST:event_buttonOkActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonOk;
    private javax.swing.JComboBox comboSucu1;
    private javax.swing.JComboBox comboSucu2;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JLabel textCompany;
    private javax.swing.JLabel textSucursal1;
    private javax.swing.JLabel textSucursal2;
    private javax.swing.JLabel textSucursales;
    // End of variables declaration//GEN-END:variables
}
